package org.petstore.web.servlet;

import com.alibaba.fastjson.JSON;
import org.petstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class checkCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/json");//以json形式输出
        PrintWriter out=resp.getWriter();
        Result result1=new Result();
        String value1=req.getParameter("vCode");
        if(value1==null||value1.equals(""))
        {
            result1.setCode(3);
            result1.setMsg("code is null");
        }else {
            String value2=(String)session.getAttribute("checkcode");
            if(value2.equalsIgnoreCase(value1)){//验证码正确
                result1.setCode(4);
                result1.setMsg("code is right");
            }else{
                result1.setCode(5);
                result1.setMsg("code is error");
            }
        }
        String str = JSON.toJSONString(result1);
        out.print(str);
        out.flush();
        out.close();
    }
}
